package com.gdones.gpt.bus.gptapi.controller;

import com.gdones.common.core.annotations.MySysLog;
import com.gdones.common.core.annotations.MySysLogOpration;
import com.gdones.common.core.dto.MyResult;
import com.gdones.gpt.bus.gptapi.dto.GptChatDto;
import com.gdones.gpt.bus.gptapi.dto.GptVideoTraclateDto;
import com.gdones.gpt.bus.gptapi.service.GptApiService;
import com.gdones.gpt.bus.webAccount.service.IGptWebaccountService;
import com.gdones.gpt.core.util.MyRedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * ChatGPT接口
 * 2023/5/30
 */
@RestController
@RequestMapping("/gptapi")
@Slf4j
@Api(tags = "ChatGPT接口")
public class GptApiController {

    //@Resource(name = "gptWebaccountServiceImpl")
    private IGptWebaccountService gptWebaccountServiceImpl;

    //@Autowired
    private MyRedisUtil redisUtil;

    //@Resource(name = "gptServiceImpl")
    private GptApiService gptApiService;

    @Autowired
    public GptApiController(GptApiService gptApiService,IGptWebaccountService gptWebaccountServiceImpl,MyRedisUtil redisUtil){
        this.gptApiService = gptApiService;
        this.gptWebaccountServiceImpl = gptWebaccountServiceImpl;
        this.redisUtil = redisUtil;

        // 初始化时将库存中所有apikey加载到redis
        // 1. 先更新数据
          gptWebaccountServiceImpl.updateAllGptLimit();
        // 2. 查询所有有余额的有秘钥的数据保存到redis中
        List<String> apiKeys =  gptWebaccountServiceImpl.getCanUseGptApiKeys();

        redisUtil.set("gptapis",apiKeys);
        redisUtil.set("gptapis_index",0);
        redisUtil.set("nowApiKey",apiKeys.get(0));
    }

    /**
     * GPT-获取模型列表
     * @return
     */
    @GetMapping("/getGptModels")
    @ApiOperation("ChatGPT接口-获取模型列表")
    @MySysLog(modelName="ChatGPT接口",methodDesc = "获取模型列表",methodType = MySysLogOpration.GET)
    public MyResult getGptModels(){

        log.debug("--ChatGPT接口：获取模型列表");
        MyResult result = new MyResult();

        try {
            result = gptApiService.getGptModels();
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("ChatGPT接口-获取模型列表-异常");
        }
        return result;
    }

    /**
     * 聊天接口
     * @param dto 聊天参数
     * @return
     */
    @PostMapping("/chat")
    @ApiOperation("ChatGPT接口-聊天接口")
    @MySysLog(modelName="ChatGPT接口",methodDesc = "聊天接口",methodType = MySysLogOpration.POST)
    public MyResult chat(@RequestBody GptChatDto dto,String staffModesID){

        log.debug("--ChatGPT接口：聊天接口");
        MyResult result = new MyResult();

        try {
            result = gptApiService.chat(staffModesID,dto);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("ChatGPT接口-聊天接口-异常");
        }
        return result;
    }

    /**
     * ChatGPT接口-gpt文生图
     * @param prompt
     * @return
     */
    @PostMapping("/chatImg")
    @ApiOperation("ChatGPT接口-gpt文生图")
    @MySysLog(modelName="ChatGPT接口",methodDesc = "gpt文生图",methodType = MySysLogOpration.POST)
    public MyResult chatImg(String prompt,String staffModesID){

        log.debug("--ChatGPT接口：gpt文生图");
        MyResult result = new MyResult();

        try {
            result = gptApiService.chatImg(prompt,staffModesID);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("ChatGPT接口-gpt文生图-异常");
        }
        return result;
    }

    @PostMapping("/stabilityImg")
    @ApiOperation("stabilit接口-文生图")
    @MySysLog(modelName="stabilit接口",methodDesc = "文生图",methodType = MySysLogOpration.POST)
    public MyResult stabilityImg(String prompt,String staffModesID){

        log.debug("--stabilit接口：文生图");
        MyResult result = new MyResult();

        try {
            result = gptApiService.stabilityImg(prompt,staffModesID);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("stabilit接口-文生图-异常");
        }
        return result;
    }

    @PostMapping("/pollinationsImg")
    @ApiOperation("pollinations接口-文生图")
    @MySysLog(modelName="pollinations接口",methodDesc = "文生图",methodType = MySysLogOpration.POST)
    public MyResult pollinationsImg(String prompt,String staffModesID){

        log.debug("--pollinations接口：文生图");
        MyResult result = new MyResult();

        try {
            result = gptApiService.pollinationsImg(prompt,staffModesID);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("pollinations接口-文生图-异常");
        }
        return result;
    }


    @PostMapping("/gptVidoTraceLate")
    @ApiOperation("GPT接口-视频提取文字翻译")
    @MySysLog(modelName="GPT接口",methodDesc = "视频提取文字翻译",methodType = MySysLogOpration.POST)
    public MyResult gptVidoTraceLate(MultipartFile file,Boolean isEnglish ,String staffModesID){
        log.debug("--GPT接口：视频提取文字翻译");
        MyResult result = new MyResult();

        try {
            result = gptApiService.gptVidoTraceLate(file,isEnglish,staffModesID);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("9999");
            result.setMsg("GPT接口-视频提取文字翻译-异常");
        }
        return result;
    }

}
